<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二叉树的层序遍历 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-content {
            padding: 20px;
            overflow-x: auto;
        }
        .code-content pre {
            margin: 0;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        .keyword { color: #569cd6; }
        .type { color: #4ec9b0; }
        .comment { color: #6a9955; }
        .string { color: #ce9178; }
        .number { color: #b5cea8; }
        .function { color: #dcdcaa; }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto animate-fadeInUp">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">二叉树的层序遍历</h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    掌握广度优先搜索的精髓，优雅地遍历二叉树的每一层
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i>阅读时间：5分钟
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-chart-line mr-2"></i>难度：中等
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.2s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-question-circle text-purple-600 text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
                </div>
                <p class="text-lg text-gray-700 leading-relaxed">
                    <span class="drop-cap serif-font">给</span>定一个二叉树，返回其按层序遍历得到的节点值（即逐层地，从左到右访问所有节点）。例如，给定二叉树 [3,9,20,null,null,15,7]，返回其层序遍历结果为 [[3], [9,20], [15,7]]。
                </p>
            </div>
        </section>

        <!-- Visual Example -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.3s;">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h3 class="text-2xl font-bold text-gray-800 mb-6 text-center">可视化示例</h3>
                <div class="mermaid">
                    graph TD
                    A[3] --> B[9]
                    A --> C[20]
                    C --> D[15]
                    C --> E[7]
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:3px,color:#fff
                    style C fill:#764ba2,stroke:#fff,stroke-width:3px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:3px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:3px,color:#fff
                </div>
                <div class="mt-6 text-center">
                    <p class="text-gray-600">层序遍历结果：<span class="font-mono bg-purple-100 px-3 py-1 rounded">[[3], [9,20], [15,7]]</span></p>
                </div>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.4s;">
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-brain text-blue-600"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">核心算法</h3>
                    </div>
                    <p class="text-gray-700 mb-4">广度优先搜索（BFS）</p>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>使用队列数据结构</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>逐层访问节点</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>保证遍历顺序</li>
                    </ul>
                </div>
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-tachometer-alt text-green-600"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">复杂度分析</h3>
                    </div>
                    <div class="space-y-3">
                        <div class="flex items-center">
                            <span class="text-gray-600 mr-2">时间复杂度：</span>
                            <span class="font-mono bg-green-100 px-3 py-1 rounded text-green-800">O(n)</span>
                        </div>
                        <div class="flex items-center">
                            <span class="text-gray-600 mr-2">空间复杂度：</span>
                            <span class="font-mono bg-green-100 px-3 py-1 rounded text-green-800">O(w)</span>
                        </div>
                        <p class="text-sm text-gray-500 mt-2">其中 n 为节点数，w 为树的最大宽度</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Solution Approach -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.5s;">
            <div class="bg-gradient-to-r from-purple-50 to-blue-50 rounded-2xl p-8">
                <h3 class="text-2xl font-bold text-gray-800 mb-6">
                    <i class="fas fa-lightbulb text-yellow-500 mr-3"></i>解题思路
                </h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white rounded-xl p-6 text-center card-hover">
                        <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <span class="text-2xl font-bold text-purple-600">1</span>
                        </div>
                        <h4 class="font-bold text-gray-800 mb-2">初始化队列</h4>
                        <p class="text-gray-600 text-sm">将根节点加入队列，开始层序遍历</p>
                    </div>
                    <div class="bg-white rounded-xl p-6 text-center card-hover">
                        <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <span class="text-2xl font-bold text-purple-600">2</span>
                        </div>
                        <h4 class="font-bold text-gray-800 mb-2">逐层处理</h4>
                        <p class="text-gray-600 text-sm">记录每层节点数，依次处理当前层所有节点</p>
                    </div>
                    <div class="bg-white rounded-xl p-6 text-center card-hover">
                        <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <span class="text-2xl font-bold text-purple-600">3</span>
                        </div>
                        <h4 class="font-bold text-gray-800 mb-2">收集结果</h4>
                        <p class="text-gray-600 text-sm">将每层节点值存入结果列表，继续下一层</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.6s;">
            <h3 class="text-2xl font-bold text-gray-800 mb-6">
                <i class="fas fa-code text-purple-600 mr-3"></i>代码实现
            </h3>
            <div class="code-block">
                <div class="code-header">
                    <div class="flex items-center">
                        <i class="fab fa-java text-orange-400 mr-2"></i>
                        <span class="text-gray-300">Java</span>
                    </div>
                    <button class="text-gray-400 hover:text-white transition-colors">
                        <i class="fas fa-copy"></i>
                    </button>
                </div>
                <div class="code-content">
                    <pre><code><span class="comment">/**
 * 二叉树节点定义
 */</span>
<span class="keyword">public class</span> <span class="type">TreeNode</span> {
    <span class="keyword">int</span> val;
    <span class="type">TreeNode</span> left;
    <span class="type">TreeNode</span> right;
    <span class="function">TreeNode</span>(<span class="keyword">int</span> x) { val = x; }
}

<span class="keyword">public</span> <span class="type">List&lt;List&lt;Integer&gt;&gt;</span> <span class="function">levelOrder</span>(<span class="type">TreeNode</span> root) {
    <span class="comment">// 存储层序遍历结果的列表</span>
    <span class="type">List&lt;List&lt;Integer&gt;&gt;</span> result = <span class="keyword">new</span> <span class="type">ArrayList</span>&lt;&gt;();
    
    <span class="comment">// 如果根节点为空，直接返回空列表</span>
    <span class="keyword">if</span> (root == <span class="keyword">null</span>) {
        <span class="keyword">return</span> result;
    }
    
    <span class="comment">// 使用队列进行广度优先搜索</span>
    <span class="type">Queue&lt;TreeNode&gt;